class GAME : public ENEMY {
    public:
        GAME();
        ~GAME();
        
        int wave;
        int level;
        OSL_IMAGE * background;        
        
        void NextWave();
        void HANDLE();
        void ShowLoadScreen();
        IMAGE * AdjustBackground();
        void GameOver(const int result);
        
    private:
        void FreeRAM();
        
}handle;

void GAME::HANDLE()
{
    //these two if statements move the whole game along
    if(numOfEnemies <= 0){
        numOfEnemies = 0;
        wave++;
        if(wave <= MAX_WAVES){FreeRAM(); NextWave();}
    }
    if(wave > MAX_WAVES){
        wave = 0;
        level++;
        AdjustBackground();
        FreeRAM();
        NextWave();
    }
    
    //draw the background image
    oslDrawImage(background);
    
    //updates all zombies
    for(int i = 0; i < 100; i++)
       if(zombie[i].alive) zombie[i].Update();
    
    //updates all green enemies
    for(int i = 0; i < 100; i++)
       if(greenEnemy[i].alive) greenEnemy[i].Update();
       
    //updates all yellow enemies
    for(int i = 0; i < 100; i++)
       if(yellowEnemy[i].alive) yellowEnemy[i].Update();
        
    return;
}

void GAME::NextWave()
{
    int numOfZombies = 0;
    int numOfGreenEnemies = 0;
    int numOfYellowEnemies = 0;
    
    if(level == 1 && wave == 1) {
        numOfZombies = 45;
    }
    
    /* ELSE USE THE ENEMY GENERATOR!! FOREVER SPAWNS ENEMIES */
    else {
        int random_number;
        srand((unsigned)time(0));
        random_number = (rand()%30)+1;
        if(random_number == 0) random_number++;
        
        numOfZombies = random_number;
    }
    
    //spawn a set number of zombies
    for(int i = 0; i < numOfZombies; i++) {
            numOfEnemies++;
            zombie[i].ai = ZOMBIE;
            zombie[i].health = 75;
            zombie[i].attack = 8;
            zombie[i].jumpHeight = 8;
            zombie[i].image = LoadImage(ZOMBIE);
            zombie[i].position = FACING_LEFT;
            if(!zombie[i].image)oslFatalError("ERROR CREATING ENEMY!");
            zombie[i].alive = Spawn(zombie[i].image, zombie[i].image->x, zombie[i].image->y);
    }
    
    //spawn a set number of green enemies
    for(int i = 0; i < numOfGreenEnemies; i++) {
            numOfEnemies++;
            greenEnemy[i].ai = GREEN_ENEMY;
            greenEnemy[i].health = 100;
            greenEnemy[i].attack = 10;
            greenEnemy[i].jumpHeight = 10;
            greenEnemy[i].image = LoadImage(GREEN_ENEMY);
            greenEnemy[i].position = FACING_LEFT;
            if(!greenEnemy[i].image)oslFatalError("ERROR CREATING ENEMY!");
            greenEnemy[i].alive = Spawn(greenEnemy[i].image, greenEnemy[i].image->x, greenEnemy[i].image->y);
    }
    
    //spawn a set number of yellow enemies
    for(int i = 0; i < numOfYellowEnemies; i++) {
            numOfEnemies++;
            yellowEnemy[i].ai = YELLOW_ENEMY;
            yellowEnemy[i].health = 125;
            yellowEnemy[i].attack = 12;
            yellowEnemy[i].jumpHeight = 12;
            yellowEnemy[i].image = LoadImage(YELLOW_ENEMY);
            yellowEnemy[i].position = FACING_LEFT;
            if(!yellowEnemy[i].image)oslFatalError("ERROR CREATING ENEMY!");
            yellowEnemy[i].alive = Spawn(yellowEnemy[i].image, yellowEnemy[i].image->x, yellowEnemy[i].image->y);
    }
    
    return;
}

void GAME::FreeRAM()
{
    //free zombie enemy RAM
    for(unsigned i = 0; i < 100; i++){
        if(!zombie[i].alive && zombie[i].image != NULL)
        {oslDeleteImage(zombie[i].image);}
    }
    
    //free green enemy RAM
    for(unsigned i = 0; i < 100; i++){
        if(!greenEnemy[i].alive && greenEnemy[i].image != NULL)
        {oslDeleteImage(greenEnemy[i].image);}
    }
    
    //free yellow enemy RAM
    for(unsigned i = 0; i < 100; i++){
        if(!yellowEnemy[i].alive && yellowEnemy[i].image != NULL)
        {oslDeleteImage(yellowEnemy[i].image);}
    }
    return;
}

void GAME::ShowLoadScreen()
{
    OSL_IMAGE * wait_screen = NULL;
            
    int random_number;
    srand((unsigned)time(0));
    random_number = (rand()%10)+1;
    
    //apply a value to the load screen
    if(random_number == 1)       wait_screen = oslLoadImageFile((char*)"img/data/loading/load1.png", OSL_IN_RAM, OSL_PF_5551);
    else if(random_number == 2)  wait_screen = oslLoadImageFile((char*)"img/data/loading/load2.png", OSL_IN_RAM, OSL_PF_5551);
    else if(random_number == 3)  wait_screen = oslLoadImageFile((char*)"img/data/loading/load3.png", OSL_IN_RAM, OSL_PF_5551);
    else if(random_number == 4)  wait_screen = oslLoadImageFile((char*)"img/data/loading/load4.png", OSL_IN_RAM, OSL_PF_5551);
    else if(random_number == 5)  wait_screen = oslLoadImageFile((char*)"img/data/loading/load5.png", OSL_IN_RAM, OSL_PF_5551);
    else if(random_number == 6)  wait_screen = oslLoadImageFile((char*)"img/data/loading/load6.png", OSL_IN_RAM, OSL_PF_5551);
    else if(random_number == 7)  wait_screen = oslLoadImageFile((char*)"img/data/loading/load7.png", OSL_IN_RAM, OSL_PF_5551);
    else if(random_number == 8)  wait_screen = oslLoadImageFile((char*)"img/data/loading/load8.png", OSL_IN_RAM, OSL_PF_5551);
    else if(random_number == 9)  wait_screen = oslLoadImageFile((char*)"img/data/loading/load9.png", OSL_IN_RAM, OSL_PF_5551);
    else if(random_number == 10) wait_screen = oslLoadImageFile((char*)"img/data/loading/load10.png", OSL_IN_RAM, OSL_PF_5551);
    else wait_screen = oslLoadImageFile((char*)"img/data/loading/load11.png", OSL_IN_RAM, OSL_PF_5551);
    
    //force load screen to show briefly
    for(int i = 0; i < 100; i++){
       oslStartDrawing();
       oslDrawImage(wait_screen);
       oslEndDrawing();
       oslSyncFrame();
    }
    
    //delete load screen
    if(wait_screen != NULL){oslDeleteImage(wait_screen); wait_screen = NULL;}
    
    return;
}

void GAME::GameOver(const int result)
{
     #define WIN 1
     #define LOSE 0
     
     //oslStopSound(gameMusic);
     
     //load images
     OSL_IMAGE * win_pic = oslLoadImageFile((char*)"img/data/win.png", OSL_IN_RAM, OSL_PF_5551);
     OSL_IMAGE * lose_pic = oslLoadImageFile((char*)"img/data/lose.png", OSL_IN_RAM, OSL_PF_5551);
     
     //brief pause
     for(int i = 0; i < 400; i++){oslStartDrawing(); oslClearScreen(BLACK); oslEndDrawing(); oslSyncFrame();}
     
     if(result == WIN){
            //oslPlaySound(bought_item, 5);
            for(int i = 0; i < 400; i++){oslStartDrawing(); oslClearScreen(BLACK); oslDrawImage(win_pic); oslEndDrawing(); oslSyncFrame();}
     }
     
     else if(result == LOSE){
            //oslPlaySound(roar, 5); oslPlaySound(roar2, 3); oslPlaySound(roar3, 6);
            for(int i = 0; i < 400; i++){oslStartDrawing(); oslClearScreen(BLACK); oslDrawImage(lose_pic); oslEndDrawing(); oslSyncFrame();}
     }
     
     else oslWarning("ERROR! WRONG PARAMETER PASSED TO void CONTROLLER::GameOver(const int result)!!!");
     
     if(win_pic != NULL){oslDeleteImage(win_pic); win_pic = NULL;}
     if(lose_pic != NULL){oslDeleteImage(lose_pic); lose_pic = NULL;}
     
     return;
}

IMAGE * GAME::AdjustBackground()
{
    //if background is already loaded, free ram.
    if(background != NULL){oslDeleteImage(background); background = NULL;}
    
    if(level == 1 || level == 2 || level == 3)
        background = oslLoadImageFilePNG((char*)"img/data/backgrounds/background1.png", OSL_IN_RAM, OSL_PF_5551);
        
    else if(level == 4 || level == 5 || level == 6)
        background = oslLoadImageFilePNG((char*)"img/data/backgrounds/background2.png", OSL_IN_RAM, OSL_PF_5551);
        
    else if(level == 7 || level == 8 || level == 9)
        background = oslLoadImageFilePNG((char*)"img/data/backgrounds/background3.png", OSL_IN_RAM, OSL_PF_5551);
        
    else if(level == 10 || level == 11 || level == 12)
        background = oslLoadImageFilePNG((char*)"img/data/backgrounds/background4.png", OSL_IN_RAM, OSL_PF_5551);
        
    else if(level == 13 || level == 14 || level == 15)
        background = oslLoadImageFilePNG((char*)"img/data/backgrounds/background5.png", OSL_IN_RAM, OSL_PF_5551);
        
    else if(level == 16 || level == 17 || level == 18)
        background = oslLoadImageFilePNG((char*)"img/data/backgrounds/background6.png", OSL_IN_RAM, OSL_PF_5551);
        
    else if(level == 17 || level == 18 || level == 19)
        background = oslLoadImageFilePNG((char*)"img/data/backgrounds/background7.png", OSL_IN_RAM, OSL_PF_5551);
        
    else if(level == 20 || level == 21 || level == 22)
        background = oslLoadImageFilePNG((char*)"img/data/backgrounds/background8.png", OSL_IN_RAM, OSL_PF_5551);
        
    else if(level == 23 || level == 24 || level == 25)
        background = oslLoadImageFilePNG((char*)"img/data/backgrounds/background9.png", OSL_IN_RAM, OSL_PF_5551);
        
    else if(level == 26 || level == 27 || level == 28)
        background = oslLoadImageFilePNG((char*)"img/data/backgrounds/background10.png", OSL_IN_RAM, OSL_PF_5551);
        
    else if(level == 29 || level == 30 || level == 31)
        background = oslLoadImageFilePNG((char*)"img/data/backgrounds/background11.png", OSL_IN_RAM, OSL_PF_5551);
        
    else if(level == 32 || level == 33 || level == 34)
        background = oslLoadImageFilePNG((char*)"img/data/backgrounds/background12.png", OSL_IN_RAM, OSL_PF_5551);
        
    else if(level == 35 || level == 36 || level == 37)
        background = oslLoadImageFilePNG((char*)"img/data/backgrounds/background13.png", OSL_IN_RAM, OSL_PF_5551);
        
    else if(level == 38 || level == 39 || level == 40)
        background = oslLoadImageFilePNG((char*)"img/data/backgrounds/background14.png", OSL_IN_RAM, OSL_PF_5551);
        

    else background = oslLoadImageFilePNG((char*)"img/data/backgrounds/background1.png", OSL_IN_RAM, OSL_PF_5551);
    

    if(!background) oslFatalError("ERROR! Background image file Missing!!!");

    return background;
}

GAME::GAME()
{
    level = 1;
    wave = 1;
    return;
}

GAME::~GAME()
{
    return;
}
